package com.ruoyi.web.controller.guarantee;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.guarantee.domain.BBidding;
import com.ruoyi.guarantee.domain.BBiddingCheck;
import com.ruoyi.guarantee.service.IBBiddingCheckService;
import com.ruoyi.guarantee.service.IBBiddingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/********************************************************************************
 ** author： zhangtianhe
 ** date：2024年11月07日 17:04
 ** description&desc：
 *********************************************************************************/
@RestController
@RequestMapping("/guarantee/biddingCheck")
public class BBiddingCheckController extends BaseController {
    @Autowired
    private IBBiddingService bBiddingService;
    @Autowired
    private IBBiddingCheckService biddingCheckService;


    //状态 0 未提交  2提交主管审核  4 业务总审核（主管提交） 6财务确认收钱 8运营确认送审银行（不进行下一步）10运营上传保函提交， 12财务核算成本 14 完成。-1 驳回
    /**
     * 业务主管待审核列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:list')")
    @GetMapping("/yyzgList")
    public TableDataInfo yyzgList(BBidding bBidding)
    {
        bBidding.setStatus(2L);//业务员提交的
        startPage();
        List<BBidding> list = bBiddingService.selectyyzgBBiddingList(bBidding);
        return getDataTable(list);
    }

    /**
     * 业务总待审核列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:list')")
    @GetMapping("/yyzList")
    public TableDataInfo yyzList(BBidding bBidding)
    {
        bBidding.setStatus(4L);//只审核业务主管订单
        startPage();
        List<BBidding> list = bBiddingService.selectBBiddingList(bBidding);
        return getDataTable(list);
    }


    /**
     * 财务待确认收到钱审核列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:list')")
    @GetMapping("/cwqList")
    public TableDataInfo cwqList(BBidding bBidding)
    {
        bBidding.setStatus(6L);
        startPage();
        List<BBidding> list = bBiddingService.selectBBiddingList(bBidding);
        return getDataTable(list);
    }

    /**
     * 运营待送审银行审核列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:list')")
    @GetMapping("/yyBList")
    public TableDataInfo yyBList(BBidding bBidding)
    {
        bBidding.setStatus(8L);
        startPage();
        List<BBidding> list = bBiddingService.selectBBiddingList(bBidding);
        return getDataTable(list);
    }

    /**
     * 提交投标保函
     */
    @Log(title = "投标保函", businessType = BusinessType.INSERT)
    @PostMapping("/yybSave/{id}")
    public AjaxResult yybSave(@PathVariable Long id)
    {
        BBidding bBidding = bBiddingService.selectBBiddingById(id);
        bBidding.setStatus(10L);
        bBidding.setUpdateBy(getUsername());
        bBidding.setUpdateTime(new Date());
        return toAjax(bBiddingService.updateBBiddingStatus(bBidding));
    }

    /**
     * 运营待出保函列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:list')")
    @GetMapping("/yyHList")
    public TableDataInfo yyHList(BBidding bBidding)
    {
        bBidding.setStatus(10L);
        startPage();
        List<BBidding> list = bBiddingService.selectBBiddingList(bBidding);
        return getDataTable(list);
    }

    /**
     * 运营待出保函保存
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:edit')")
    @Log(title = "投标保函", businessType = BusinessType.UPDATE)
    @PostMapping("/yyHSave")
    public AjaxResult yyHSave(@RequestBody BBidding bBidding)
    {
        if (bBidding.getCheckStatus()==-1){//驳回
            bBidding.setRecallMind(bBidding.getCheckMind());
            bBidding.setRecallTime(new Date());
            bBidding.setRejectUserName(getUsername());

        }
        BBidding oldBidding = bBiddingService.selectBBiddingById(bBidding.getId());

        BBiddingCheck bBiddingCheck = new BBiddingCheck();
        bBiddingCheck.setBiddingId(bBidding.getId());
        bBiddingCheck.setCheckMind(bBidding.getCheckMind());
        bBiddingCheck.setOldStatus(oldBidding.getStatus());
        bBiddingCheck.setCheckStatus(bBidding.getCheckStatus());
        bBiddingCheck.setCreateBy(getUsername());
        bBiddingCheck.setCreateTime(new Date());
        biddingCheckService.insertBBiddingCheck(bBiddingCheck);

        bBidding.setStatus(bBidding.getCheckStatus());
        return toAjax(bBiddingService.updateBBidding(bBidding));
    }

    /**
     * 财务待确认公司收益成本审核保存
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:edit')")
    @Log(title = "投标保函", businessType = BusinessType.UPDATE)
    @PostMapping("/cwcSave")
    public AjaxResult cwcSave(@RequestBody BBidding bBidding)
    {
        if (bBidding.getCheckStatus()==-1){//驳回
            bBidding.setRecallMind(bBidding.getCheckMind());
            bBidding.setRecallTime(new Date());
            bBidding.setRejectUserName(getUsername());

        }
        BBidding oldBidding = bBiddingService.selectBBiddingById(bBidding.getId());

        BBiddingCheck bBiddingCheck = new BBiddingCheck();
        bBiddingCheck.setBiddingId(bBidding.getId());
        bBiddingCheck.setCheckMind(bBidding.getCheckMind());
        bBiddingCheck.setOldStatus(oldBidding.getStatus());
        bBiddingCheck.setCheckStatus(bBidding.getCheckStatus());
        bBiddingCheck.setCreateBy(getUsername());
        bBiddingCheck.setCreateTime(new Date());
        biddingCheckService.insertBBiddingCheck(bBiddingCheck);

        bBidding.setStatus(bBidding.getCheckStatus());
        return toAjax(bBiddingService.updateBBidding(bBidding));
    }
    /**
     * 财务待确认公司收益成本审核列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:list')")
    @GetMapping("/cwcList")
    public TableDataInfo cwcList(BBidding bBidding)
    {
        bBidding.setStatus(12L);
        startPage();
        List<BBidding> list = bBiddingService.selectBBiddingList(bBidding);
        return getDataTable(list);
    }


    /**
     * 审核保存
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:edit')")
    @PostMapping("/yyzgCheck")
    public AjaxResult yyzgCheck(@RequestBody BBiddingCheck bBiddingCheck)
    {
        //查询订单
        BBidding bBidding = bBiddingService.selectBBiddingById(bBiddingCheck.getBiddingId());
        bBidding.setStatus(bBiddingCheck.getCheckStatus());
        bBiddingCheck.setOldStatus(bBidding.getStatus());
        if (bBiddingCheck.getCheckStatus()==-1){//驳回
            bBidding.setRecallMind(bBiddingCheck.getCheckMind());
            bBidding.setRecallTime(new Date());
            bBidding.setRejectUserName(getUsername());

        }
        bBidding.setUpdateTime(new Date());
        bBiddingService.updateBBidding(bBidding);
        return toAjax(biddingCheckService.insertBBiddingCheck(bBiddingCheck));
    }


    /**
     * 审核保存
     */
    @PreAuthorize("@ss.hasPermi('guarantee:bidding:edit')")
    @PostMapping("/checkSave")
    public AjaxResult checkSave(@RequestBody BBiddingCheck bBiddingCheck)
    {
        //查询订单
        BBidding bBidding = bBiddingService.selectBBiddingById(bBiddingCheck.getBiddingId());
        bBidding.setStatus(bBiddingCheck.getCheckStatus());
        bBiddingCheck.setOldStatus(bBidding.getStatus());
        if (bBiddingCheck.getCheckStatus()==-1){//驳回
            bBidding.setRecallMind(bBiddingCheck.getCheckMind());
            bBidding.setRecallTime(new Date());
            bBidding.setRejectUserName(getUsername());

        }
        bBidding.setUpdateTime(new Date());
        bBiddingService.updateBBiddingStatus(bBidding);
        return toAjax(biddingCheckService.insertBBiddingCheck(bBiddingCheck));
    }

    /**
     * 修改投标保函
     */
    @PostMapping("/updateBidding")
    public AjaxResult updateBidding(@RequestBody BBidding bBidding)
    {
        return toAjax(bBiddingService.updateBBiddingStatus(bBidding));
    }

}
